package com.jy.api.dao;

import com.jy.api.signUpReq.stock.RecordDetailsSignUpReq;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2018/2/3.
 */
public class StockTransactionDaoImpl {


    @Autowired
    JdbcTemplate jdbcTemplate;


    public List<Map<String, Object>> queryStockDetails(RecordDetailsSignUpReq signUpReq) {
        Map<String, Object> map = new HashMap();
        map.put("storeId", signUpReq.getStoreId());

        map.put("startNumber", signUpReq.getPage() * signUpReq.getSize());
        map.put("endNumber", signUpReq.getSize());
        String sql = " select product_name productName,whole,single," +
                " stock_quantity_ratio stockQuantityRatio,`type`,source,notes," +
                " oper_time operTime,stock_price stockPrice,operator_name operatorName" +
                " from  stock_transaction WHERE  store_id=:storeId ";

        if (StringUtils.isNotBlank(signUpReq.getFrom()) && StringUtils.isNotBlank(signUpReq.getTo())) {
            sql += " and oper_time BETWEEN :startTime and :endTime ";
            map.put("startTime", MyDateUtils.getDateFormatDd(signUpReq.getFrom(),
                    "yyyy-MM-dd HH:mm:ss"));
            map.put("endTime", MyDateUtils.getDateFormatDd(signUpReq.getTo(),
                    "yyyy-MM-dd HH:mm:ss"));
        }


        if (StringUtils.isNotBlank(signUpReq.getProductId())) {
            sql += " and product_id=:productId ";
            map.put("productId", signUpReq.getProductId());
        }

        if (StringUtils.isNotBlank(signUpReq.getStockType())) {
            sql += " and `type`=:stockType ";
            map.put("stockType", signUpReq.getStockType());
        }

        if (StringUtils.isNotBlank(signUpReq.getStockSource())) {
            sql += " and `source`=:stockSource ";
            map.put("stockSource", signUpReq.getStockSource());
        }

        if (StringUtils.isNotBlank(signUpReq.getOperatorId())) {
            sql += " and `operator_id`=:operatorId ";
            map.put("operatorId", signUpReq.getOperatorId());
        }
        sql += " order by oper_time DESC limit :startNumber,:endNumber";
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
        return namedParameterJdbcTemplate.queryForList(sql, map);
    }


    public long queryStockDetailsNumber(RecordDetailsSignUpReq signUpReq) {
        Map<String, Object> map = new HashMap();
        map.put("storeId", signUpReq.getStoreId());

        String sql = " select count(*) from  stock_transaction WHERE  store_id=:storeId ";

        if (StringUtils.isNotBlank(signUpReq.getFrom()) && StringUtils.isNotBlank(signUpReq.getTo())) {
            sql += " and oper_time BETWEEN :startTime and :endTime ";
            map.put("startTime", MyDateUtils.getDateFormatDd(signUpReq.getFrom(),
                    "yyyy-MM-dd HH:mm:ss"));
            map.put("endTime", MyDateUtils.getDateFormatDd(signUpReq.getTo(),
                    "yyyy-MM-dd HH:mm:ss"));
        }

        if (StringUtils.isNotBlank(signUpReq.getProductId())) {
            sql += " and product_id=:productId ";
            map.put("productId", signUpReq.getProductId());
        }

        if (StringUtils.isNotBlank(signUpReq.getStockType())) {
            sql += " and `type`=:stockType ";
            map.put("stockType", signUpReq.getStockType());
        }

        if (StringUtils.isNotBlank(signUpReq.getStockSource())) {
            sql += " and `source`=:stockSource ";
            map.put("stockSource", signUpReq.getStockSource());
        }

        if (StringUtils.isNotBlank(signUpReq.getOperatorId())) {
            sql += " and `operator_id`=:operatorId ";
            map.put("operatorId", signUpReq.getOperatorId());
        }
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
        return namedParameterJdbcTemplate.queryForObject(sql, map, Long.class);
    }
}
